package com.rich.dao.impl;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.rich.dao.CategoryDao;
import com.rich.domain.Category;
import com.rich.utils.DataSourceUtils;

public class CategoryDaoImpl implements CategoryDao{

	QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
	/**
	 * 查询所有分类
	 */
	@Override
	public List<Category> findAll() throws Exception {
		String sql = "SELECT * FROM category";
		return qr.query(sql, new BeanListHandler<>(Category.class));
	}
	
	/**
	 * 保存分类
	 */
	@Override
	public void save(Category category) throws Exception {
		String sql = "INSERT INTO category VALUES(?,?)";
		qr.update(sql, category.getcID(),category.getcName());
	}

	/**
	 * 修改分类
	 */
	@Override
	public void update(String cid,String cName) throws Exception {
		String sql = "UPDATE category SET cname=? WHERE cid=?";
		qr.update(sql, cName,cid);
	}

	
	/**
	 * 删除某个分类
	 */
	@Override
	public void remove(String cid) throws Exception {
		String sql = "DELETE FROM category WHERE cid = ?";
		qr.update(sql, cid);
	}
}
